package org.nanotek.musicbrainz.job.base;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.springframework.batch.admin.service.JobService;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobExecutionNotRunningException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.batch.core.launch.NoSuchJobExecutionException;
import org.springframework.batch.core.launch.support.SimpleJobOperator;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.validation.Validator;

public class ArtistAliasDataLoad {

	private static ClassPathXmlApplicationContext ctx;

	/**
	 * @param args
	 * @throws JobParametersInvalidException 
	 * @throws JobInstanceAlreadyCompleteException 
	 * @throws JobRestartException 
	 * @throws JobExecutionAlreadyRunningException 
	 * @throws NoSuchJobException 
	 * @throws JobExecutionNotRunningException 
	 * @throws NoSuchJobExecutionException 
	 */
	public static void main(String[] args)  {
		ctx = new ClassPathXmlApplicationContext(new String[] {
		"/spring-batch-jobs/artist_alias/spring-batch-artist-context.xml"});
		Job artistCreditJob = (Job) ctx.getBean("artistAliasIndexJob");
		boolean started = false; 
		//		JobService jobService = (JobService) ctx.getBean("jobService");
		//		jobService.stopAll();
		SimpleJobOperator jobOperator =   (SimpleJobOperator) ctx.getBean("jobOperator");
		JobExplorer jobExplorer = (JobExplorer) ctx.getBean("jobExplorer");
//		Validator validator = (Validator) ctx.getBean("validator");
		try { 
			JobParameters jobParameters = new JobParameters();
			JobParameter jobParameter = new JobParameter(new Date());
//			List<String> jobNames = jobExplorer.getJobNames();
//			for (String jobName:jobNames) { 
//			    if (jobName.equals("artistAliasIndexJob")) {
//				List<JobInstance>jobInstances = jobExplorer.getJobInstances(jobName, 0, 1000);
//				for (JobInstance jobInstance:jobInstances) 
//				{ 
////					if (jobName.equals("artistAliasIndexJob"))
////					{ 
//						List<Long> executions = jobOperator.getExecutions(jobInstance.getId());
//						for (Long execution : executions) 
//						{ 
//							JobExecution jobExecution = jobExplorer.getJobExecution(execution);
//							jobExecution.upgradeStatus(BatchStatus.FAILED);
//							jobExecution.stop();
//							started = true;
//							jobOperator.restart(execution);
//						}
////					}else { 
////						jobOperator.abandon(jobInstance.getId());
////					} 
//				}
//					Set<Long> executions = jobOperator.getRunningExecutions(jobName);
//					java.util.Iterator<Long> it = executions.iterator();
//					while (it.hasNext()){
//						JobExecution execution = jobExplorer.getJobExecution(it.next());
//						execution.setStatus(BatchStatus.ABANDONED);
//						execution.stop();
//					}
//				}
//			}
//			if (!started)
//				jobOperator.start("artistAliasIndexJob", jobParameters.toString());
			JobLauncher jobLauncher = (JobLauncher) ctx.getBean("jobLauncher");
			jobLauncher.run(artistCreditJob, new JobParametersBuilder().addLong("run.id", System.currentTimeMillis()).toJobParameters());
		}catch (Exception ex) 
		{ 
			ex.printStackTrace();
		}
	}

	//	public JobExecution abandon(Long jobExecutionId) throws NoSuchJobExecutionException,
	//	JobExecutionAlreadyRunningException {
	//
	//		JobExecution jobExecution = getJobExecution(jobExecutionId);
	//		if (jobExecution.getStatus().isLessThan(BatchStatus.STOPPING)) {
	//			throw new JobExecutionAlreadyRunningException(
	//					"JobExecution is running or complete and therefore cannot be aborted");
	//		}
	//
	//		logger.info("Aborting job execution: " + jobExecution);
	//		jobExecution.upgradeStatus(BatchStatus.ABANDONED);
	//		jobRepository.update(jobExecution);
	//		return jobExecution;
	//
	//	}

}
